from rest_framework.generics import ListAPIView, GenericAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response

from goods.models import SPU, SPUSpecification

from meiduo_admin.serializers.spus import SPUSimpleSerializer, SPUSpecSerializer


#GET /meiduo_admin/goods/simple/
class SPUSimpleView(ListAPIView):

    permission_classes = [IsAdminUser]

    #指定视图使用的查询集
    queryset = SPU.objects.all()
    #指定视图使用的序列化器类
    serializer_class = SPUSimpleSerializer
    #注：关闭分页
    pagination_class = None


# GET meiduo_admin/goods/(?P<pk>\d+)/specs/
class SPUSpecView(ListAPIView):

    pagination_class = [IsAdminUser]

    serializer_class = SPUSpecSerializer

    def get_queryset(self):
        """返回视图所使用的查询集"""

        #获取pk
        pk = self.kwargs['pk']

        return SPUSpecification.objects.filter(spu_id=pk)
    #关闭分页
    pagination_class = None

    # def get(self,request,pk):
    #     """
    #     获取spu规格选项数据：
    #     1.根据pk获取spu  specs数据
    #     2.将spu数据序列化并返回
    #     """
    #
    #     #1.根据pk获取spu specs数据
    #     specs = SPUSpecification.objects.filter(spu_id=pk)
    #
    #     #2.将spu数据序列化并返回
    #
    #     serializer = SPUSpecSerializer(specs,many=True)
    #
    #     return Response(serializer.data)
